from selenium import webdriver
import time
# By类，用于定位元素
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
try:
    base_url = 'https://www.maigoo.com/news/463085.html'
    driver.get(base_url)  # 浏览器登录
    # 显示等待
    wait = WebDriverWait(driver, 1)
    table_tr_list = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'table tbody tr')))
    tr_lists = []
    for tr_list in table_tr_list:
        tr_lists.append(tr_list.text.replace(" ", ","))
    print(tr_lists)
    # 结果写入文件
    with open("./中国企业500强名单.csv", 'w', encoding='utf-8') as f:
        for item in tr_lists:
            f.write(f"{item}\n")
except Exception as error:
    print("运行过程出错！")
    print(f"发生了错误{error}")
finally:
    time.sleep(5)
    driver.quit()  # 关闭浏览器